home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / STRG57.ARJ / RATE57.PAS < prev    next >
Pascal/Delphi Source File  |  1991-01-23  |  6KB  |  146 lines

  1. { ========================================================================== }
  2. { Rate57.pas - Rate timer using only low memory clock.     ver 5.7, 01-23-91 }
  3. {                                                                            }
  4. { This rate timer is a highly accurate timer based on the low memory clock.  }
  5. { It is device independent by not accessing any special timer hardware and   }
  6. { does not use any interrupts.  Accuracy is +-1 repetition.                  }
  7. {                                                                            }
  8. { This timer is great for timing short events (1 microsecond to .1 second)   }
  9. { to get a Reps-per-second result.  The secret to such great accuracy is     }
  10. { keying in on the fact that the low memory clock is very accurate and on    }
  11. { schedule every time it ticks.  So, just make the event work by the clock   }
  12. { rather than vice-versa.                                                    }
  13. {                                                                            }
  14. { The timer is set for a 1 second test.  If you can't get enough repetitions }
  15. { (>100) to get any resolution, you are free to change TestTime up to 3600   }
  16. { seconds.                                                                   }
  17. {                                                                            }
  18. { The timer even accounts for the overhead of the timer itself and any other }
  19. { overhead that is a part of your event.  Just insert your code and go.  You }
  20. { may experience a little jitter while running in the integrated environment,}
  21. { but is solid when run as an executable file.                               }
  22. {                                                                            }
  23. { For further explanation of the high-resolution feature of this timer, see  }
  24. { comments at the end of this file.                                          }
  25. {                                                                            }
  26. {   Public Domain by James H. LeMay, Eagle Performance Software              }
  27. { ========================================================================== }
  28. {$A+,D+,E+,L-,N+,R-,S-,V-}
  29.  
  30. program RateTimer;
  31.  
  32. uses
  33.   Crt,Strg;
  34.  
  35. const
  36.   TestTime = 1;  { seconds }
  37.   TicksPerDay = 1573040.0;            { DOS timer ticks/day. }
  38.   TicksPerSec = TicksPerDay/86400.0;
  39.  
  40. var
  41.   LowClock: word absolute $0000:$046C;
  42.   Tick1,Ticks:           word;
  43.   Reps,OverheadReps:     longint;
  44.   RepsPerSec,TickFactor: real;
  45.   R: real absolute RepsPerSec; { Variable name is easier to Watch }
  46.   { -- Place your variables here: -- }
  47.   S1,S2: string;
  48.   b:     byte;
  49.  
  50. procedure WaitForTick;
  51. begin
  52.   Tick1 := LowClock;
  53.   repeat
  54.   until LowClock<>Tick1;
  55.   Tick1 := LowClock;
  56. end;
  57.  
  58. procedure RunTime (Seconds: word);
  59. begin
  60.   if Seconds>3600
  61.     then Seconds:=3600;
  62.   Ticks := trunc(Seconds*TicksPerSec);
  63.   TickFactor := Seconds*TicksPerSec/Ticks; { Accounts for trunc of seconds }
  64.   WaitForTick;
  65. end;
  66.  
  67. procedure Init;
  68. begin
  69.   { ** Initialize any of your variables here: ** }
  70.   S1 := 'Now is the time to check out Eagle Performance Software Products'+
  71.         'for speed tests';
  72.   S2 := S1;
  73. end;
  74.  
  75. procedure ResetVariables;
  76. begin
  77.   { ** Place any variables that need to be reset after each test here: ** }
  78.   { StrMove (S2,S1); }
  79. end;
  80.  
  81. procedure Test;
  82. begin
  83.   Reps := 0;
  84.   RunTime (TestTime);
  85.   repeat
  86.     { ** Insert your test routine here ** }
  87.     b := StrPosL (S1,'speed',1);
  88.     { ** End of test routine ** }
  89.     ResetVariables;
  90.     inc (Reps);
  91.   until LowClock-Tick1>=Ticks;
  92. end;
  93.  
  94. procedure OverHead;
  95. begin
  96.   OverHeadReps := 0;
  97.   RunTime (TestTime);
  98.   repeat
  99.     ResetVariables;
  100.     inc (OverHeadReps);
  101.   until LowClock-Tick1>=Ticks;
  102. end;
  103.  
  104. procedure CalcRate;
  105. begin
  106.   RepsPerSec := TickFactor/((TestTime)*(1.0/Reps-1.0/OverHeadReps));
  107.   WriteLn ('Strings/sec = ',RepsPerSec:8:0);
  108. end;
  109.  
  110. begin
  111.   Init;
  112.   Test;
  113.   OverHead;
  114.   CalcRate;
  115. end.
  116.  
  117. { ========================================================================== }
  118. { The simplicity of the rate timer is deceptive.  Don't let it fool you.  If }
  119. { you take a just a cursory look, you may think that you can only get 1/18.2 }
  120. { second resolution out this timer.  But think again.                        }
  121. {                                                                            }
  122. { There are two clocks in your computer - (1) the 8253/8254 timer chip, and  }
  123. { (2) the CPU.  (One doesn't usually think of the CPU as a clock!)  Remember,}
  124. { the low-memory output for the clock comes from the 8253/8254 chip which is }
  125. { accurate to 1-microsecond, even though it doesn't "tick" but every         }
  126. { 1/18.2... seconds.                                                         }
  127. {                                                                            }
  128. { Since we can't get high resolution from the low-memory clock, the program  }
  129. { does the reverse and gets the high resolution from the CPU instead.  There }
  130. { are two types of applications for a timer - (1) EVENT TIMER: duration of a }
  131. { a single event, and (2) RATE TIMER: the number of events in a single       }
  132. { duration.  Our application in STRG is the latter.  Where resolution (not   }
  133. { accuracy) is low in the timer, it is gained in the CPU by making sure      }
  134. { there are a LARGE number of events.                                        }
  135. {                                                                            }
  136. { Notice that this timer is accurate to +/- 1 repetition as opposed to some  }
  137. { value in "seconds".  That's because it measures EVENTS, not seconds.  So,  }
  138. { a test of a given duration producing 100,000 events will certainly have    }
  139. { higher resolution than the same duration producing only 100 events.  The   }
  140. { former would have 0.001% resolution while the latter only 1%.  If you      }
  141. { increase the duration of the test, you can get even higher resolution.     }
  142. {                                                                            }
  143. { RATE is meant to be a test tool for STRG to give a rough idea of           }
  144. { comparative performance and works quite well.                              }
  145. { ========================================================================== }
  146.